<?php

declare(strict_types=1);

namespace app\system\controller;

use app\admin\BaseAdminController;
use think\facade\View;
use app\system\model\LogOperate as Log;
use think\facade\Request;
use app\admin\model\Data;

class Operate extends BaseAdminController
{
    /**
     * 成员操作日志页面
     */
    public function index()
    {
        View::assign('title', '操作日志/系统管理');
        View::assign('addJs', addJs(['datepicker']));
        return View::fetch();
    }

    /**
     * 导出操作日志
     */
    public function export()
    {
        if ($this->envMode == 'demo'){
            return $this->isDemo();
        }
        [$obj,] = $this->_();
        $list = $obj->order('id', 'desc')->select();
        $data = [];
        foreach ($list as $value) {
            $data[] = [
                '操作名称' => $value['name'],
                '操作人员' => $value['username'],
                '命令' => $value['command'],
                'IP' => $value['ip'],
                '操作信息' => $value['info'],
                '操作时间' => $value['create_time'],
            ];
        }
        $url = Data::export('操作日志', $data);
        $get = Request::get();
        $get['excel'] = explode('=', $url)[1];
        Log::add('导出操作日志', $get);
        return json(['url' => $url]);
    }

    /**
     * 查询操作日志
     */
    private function _()
    {
        $username = input('username');
        $name = input('name');
        $start = input('start');
        $end = input('end');
        $obj = new Log();
        if ($username) {
            $obj = $obj->where('username', $username);
        }
        if ($name) {
            $obj = $obj->where('name', $name);
        }
        if ($start) {
            $obj = $obj->where('create_time', '>=', strtotime($start . ' 00:00:00'));
        }
        if ($end) {
            $obj = $obj->where('create_time', '<=', strtotime($end . ' 23:59:59'));
        }
        $total = $obj->count();
        return [$obj, $total];
    }

    /**
     * 读取操作日志信息列表
     */
    public function data()
    {
        $offset = input('offset/d');
        $limit = input('limit/d');
        [$obj, $total] = $this->_();
        $list = $obj->order('id', 'desc')->limit($offset, $limit)->select();
        return json([
            'total' => $total,
            'rows' => $list,
        ]);
    }

    /**
     * 清空操作日志
     */
    public function clear()
    {
        if ($this->envMode == 'demo'){
            return $this->isDemo();
        }
        Log::destroy(function($query){
            $query->where('id', '>', 0);
        });
        Log::add('清空操作日志', []);
        return json(['msg' => '']);
    }

    /**
     * 查看操作日志信息
     */
    public function view()
    {
        $id = input('id/d');
        $arr = Log::find($id);
        View::assign('info', $arr);
        $data = json_decode($arr['info'], true);
        if (isset($data['excel'])) {
            $mode = explode('/', $arr['command'])[0];
            $url = url('admin/dashboard/superDownload')->build() . '?mode=' . $mode . '&file=' . $data['excel'];
            $data['excel'] = "<a href={$url}>下载</a>";
        }
        View::assign('data', var_export($data, true));
        return View::fetch();
    }
}
